package com.ruoyi.production.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.production.domain.ProductionPlan;
import com.ruoyi.production.domain.ProductionWorkOrderDetail;
import com.ruoyi.production.domain.vo.ProductionAndSaleVo;
import com.ruoyi.production.domain.vo.ProductionWorkOrderAndPlanVo;
import com.ruoyi.production.mapper.ProductionPlanMapper;
import com.ruoyi.production.mapper.ProductionWorkOrderDetailMapper;
import com.ruoyi.sale.domain.SaleOrder;
import com.ruoyi.sale.mapper.SaleOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.production.mapper.ProductionWorkOrderMapper;
import com.ruoyi.production.domain.ProductionWorkOrder;
import com.ruoyi.production.service.IProductionWorkOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 生产工单Service业务层处理
 *
 * @author jgb
 * @date 2024-10-30
 */
@Service
public class ProductionWorkOrderServiceImpl implements IProductionWorkOrderService
{
    @Autowired
    private ProductionWorkOrderMapper productionWorkOrderMapper;

    @Autowired
    private ProductionWorkOrderDetailMapper productionWorkOrderDetailMapper;

    @Autowired
    private ProductionPlanMapper productionPlanMapper;

    @Autowired
    private SaleOrderMapper saleOrderMapper;

    @Override
    public ProductionWorkOrderAndPlanVo getOrderById(Long id) {
        return productionWorkOrderMapper.selectProductionWorkOrderVoById(id);
    }

    @Override
    public List<ProductionWorkOrderAndPlanVo> selectProductionWorkOrderAndPlan(ProductionWorkOrderAndPlanVo productionWorkOrderAndPlanVo) {
        return productionWorkOrderMapper.selectProductionWorkOrderAndPlan(productionWorkOrderAndPlanVo);
    }

    /**
     * 查询生产工单
     *
     * @param id 生产工单主键
     * @return 生产工单
     */
    @Override
    public ProductionWorkOrder selectProductionWorkOrderById(Long id)
    {
        return productionWorkOrderMapper.selectProductionWorkOrderById(id);
    }

    /**
     * 查询生产工单列表
     *
     * @param productionWorkOrder 生产工单
     * @return 生产工单
     */
    @Override
    public List<ProductionWorkOrder> selectProductionWorkOrderList(ProductionWorkOrder productionWorkOrder)
    {
        return productionWorkOrderMapper.selectProductionWorkOrderList(productionWorkOrder);
    }

    /**
     * 新增生产工单
     *
     * @param productionWorkOrderAndPlanVo 生产工单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProductionWorkOrder(ProductionWorkOrderAndPlanVo productionWorkOrderAndPlanVo)
    {
        ProductionWorkOrder productionWorkOrder = new ProductionWorkOrder(null,productionWorkOrderAndPlanVo.getPlanId(),
                productionWorkOrderAndPlanVo.getWorkOrderName(),productionWorkOrderAndPlanVo.getStaffId(),
                productionWorkOrderAndPlanVo.getProductDeptId(),0,
                productionWorkOrderAndPlanVo.getProductionCount(),productionWorkOrderAndPlanVo.getProductionNo(),
                null,productionWorkOrderAndPlanVo.getPlanStartTime(),productionWorkOrderAndPlanVo.getPlanEndTime(),null);
        productionWorkOrder.setCreateTime(DateUtils.getNowDate());
        int i = productionWorkOrderMapper.insertProductionWorkOrder(productionWorkOrder);
        System.out.println("productionWorkOrder.getId() = " + productionWorkOrder.getId());
        for (ProductionWorkOrderDetail productionWorkOrderDetail : productionWorkOrderAndPlanVo.getProductionWorkOrderDetailList()) {
            productionWorkOrderDetail.setProductionWorkOrderId(Long.valueOf(productionWorkOrder.getId()));
            productionWorkOrderDetail.setStaffId(Long.valueOf(productionWorkOrderAndPlanVo.getStaffId()));
            i+=productionWorkOrderDetailMapper.insertProductionWorkOrderDetail(productionWorkOrderDetail);
        }
        return i;
    }

    /**
     * 修改生产工单
     *
     * @param productionWorkOrder 生产工单
     * @return 结果
     */
    @Override
    public int updateProductionWorkOrder(ProductionWorkOrder productionWorkOrder)
    {
        productionWorkOrder.setUpdateTime(DateUtils.getNowDate());
        return productionWorkOrderMapper.updateProductionWorkOrder(productionWorkOrder);
    }

    @Transactional
    @Override
    public int updateProductionWorkOrderStatus(ProductionWorkOrder productionWorkOrder) {
        if (productionWorkOrder.getStatus()==1){
            productionWorkOrder.setStartTime(DateUtils.getNowDate());
            int i = productionWorkOrderMapper.updateProductionWorkOrder(productionWorkOrder);
            //查询销售id
            ProductionAndSaleVo productionAndSaleVo = productionPlanMapper.selectProductionPlanById(Long.valueOf(productionWorkOrder.getPlanId()));
            SaleOrder saleOrder = new SaleOrder();
            saleOrder.setId(productionAndSaleVo.getSaleId());
            saleOrder.setStatus(5L);
            saleOrderMapper.updateSaleOrder(saleOrder);

            ProductionPlan productionPlan = new ProductionPlan();
            productionPlan.setId(Long.valueOf(productionWorkOrder.getPlanId()));
            productionPlan.setStatus(3L);
            productionPlan.setStartTime(DateUtils.getNowDate());
            int i1 = productionPlanMapper.updateProductionPlan(productionPlan);
            return i+i1;
        }else {
            productionWorkOrder.setEndTime(DateUtils.getNowDate());
            return productionWorkOrderMapper.updateProductionWorkOrder(productionWorkOrder);
        }

    }

    @Transactional
    @Override
    public int updateProductionWorkOrderAndDetail(ProductionWorkOrderAndPlanVo productionWorkOrderAndPlanVo) {
        ProductionWorkOrder productionWorkOrder = new ProductionWorkOrder(
                productionWorkOrderAndPlanVo.getId(),productionWorkOrderAndPlanVo.getPlanId(),
                productionWorkOrderAndPlanVo.getWorkOrderName(),productionWorkOrderAndPlanVo.getStaffId(),
                productionWorkOrderAndPlanVo.getProductDeptId(),0,
                productionWorkOrderAndPlanVo.getProductionCount(),productionWorkOrderAndPlanVo.getProductionNo(),
                null,productionWorkOrderAndPlanVo.getPlanStartTime(),productionWorkOrderAndPlanVo.getPlanEndTime(),null);
        productionWorkOrder.setCreateTime(DateUtils.getNowDate());
        int i = productionWorkOrderMapper.updateProductionWorkOrder(productionWorkOrder);
        productionWorkOrderDetailMapper.deleteProductionWorkOrderDetailByOrderIds(new Long[]{productionWorkOrder.getId()});
        System.out.println("productionWorkOrder.getId() = " + productionWorkOrder.getId());
        for (ProductionWorkOrderDetail productionWorkOrderDetail : productionWorkOrderAndPlanVo.getProductionWorkOrderDetailList()) {
            productionWorkOrderDetail.setProductionWorkOrderId(Long.valueOf(productionWorkOrder.getId()));
            productionWorkOrderDetail.setStaffId(Long.valueOf(productionWorkOrderAndPlanVo.getStaffId()));
            i+=productionWorkOrderDetailMapper.insertProductionWorkOrderDetail(productionWorkOrderDetail);
        }
        return i;
    }

    /**
     * 批量删除生产工单
     *
     * @param ids 需要删除的生产工单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductionWorkOrderByIds(Long[] ids)
    {
        productionWorkOrderDetailMapper.deleteProductionWorkOrderDetailByOrderIds(ids);
        return productionWorkOrderMapper.deleteProductionWorkOrderByIds(ids);
    }

    /**
     * 删除生产工单信息
     *
     * @param id 生产工单主键
     * @return 结果
     */
    @Override
    public int deleteProductionWorkOrderById(Long id)
    {
        return productionWorkOrderMapper.deleteProductionWorkOrderById(id);
    }
}
